﻿using BepInEx;
using System;
using UnityEngine;
using HarmonyLib;
using UnityEngine.UI;
using System.Reflection;
using System.Threading;
using System.Collections.Generic;

namespace tiandiguixu.foring
{

    /// <summary>
    /// 神庙刷词条
    /// </summary>
    public partial class ForingMod
    {

        [HarmonyPrefix, HarmonyPatch(typeof(SacrificePanel), "WorshipBtnOnClick")]
        public static void SacrificePanel_WorshipBtnOnClick_Prefix(SacrificePanel __instance)
        {
            sacrificePanel = __instance;
            instance.Logger.LogInfo("刷新词条前功德：" + WholeObjects.Instance.GetPlayerSect().gongDe);
            GongDe = WholeObjects.Instance.GetPlayerSect().gongDe;
        }

        [HarmonyPostfix, HarmonyPatch(typeof(SacrificePanel), "WorshipBtnOnClick")]
        public static void SacrificePanel_WorshipBtnOnClick_Postfix(SacrificePanel __instance)
        {
            instance.Logger.LogInfo("刷新词条后功德：" + WholeObjects.Instance.GetPlayerSect().gongDe);
        }

        [HarmonyPrefix, HarmonyPatch(typeof(EntryBuilder), nameof(EntryBuilder.AddACharaEntry))]
        public static void EntryBuilder_AddACharaEntry(object[] __args)
        {
            instance.Logger.LogInfo("EntryBuilder_AddACharaEntry");
            confirm = false;
            chara = __args[0] as Chara;
            instance.Logger.LogInfo("EntryBuilder_AddACharaEntry ：" + chara.name + "maxAddEntryNum " + chara.maxAddEntryNum);
        }

        private static string GetBeReplaceEntrys(string name)
        {
            if (chara == null)
            {
                instance.Logger.LogWarning("chara is null");
                return name;
            }
            var quality = AllDictionary.Dic_EntryInfo[name].quality;
            foreach (var e in chara.entrys)
            {
                var item = AllDictionary.Dic_EntryInfo[e];
                if (item.quality == 5) continue;
                if (item.quality == quality - 1)
                {
                    foreach (var entry in AllDictionary.Dic_EntryInfo.Values)
                    {
                        if (entry.quality != quality) continue;
                        if (entry.beReplaceEntrys.Contains(e))
                        {
                            return entry.name;
                        }

                    }
                }
            }
            return name;
        }

        [HarmonyPrefix, HarmonyPatch(typeof(EntryManager), nameof(EntryManager.OnAdd))]
        public static bool EntryManager_OnAdd(EntryManager __instance, object[] __args)
        {
            instance.Logger.LogInfo("EntryManager_OnAdd" + __instance);

            //判断是否创建人物界面
            if (TitleSceneCreateCharaPanel.instance != null && TitleSceneCreateCharaPanel.instance.isActiveAndEnabled)
            {
                instance.Logger.LogInfo("EntryManager_OnAdd ：创建人物界面");
                return true;
            }

            if (sacrificePanel == null || !sacrificePanel.isActiveAndEnabled) return true;

            if (__args == null) return true;
            if (__args.Length != 2) return true;
            if (__args[0].ToString().StartsWith("神愿祈祷·")) return true;
            if (__args[0].ToString().StartsWith("大道洗涤·")) return true;


            var entry = __args[0] as string;
            var newEntry = GetBeReplaceEntrys(entry);
            //instance.Logger.LogWarning(__args[0] + ":" + newEntry);
            if (newEntry != entry)
            {
                entry = newEntry;
            }

            ConfirmPanel.instance.Open(String.Format("词条：{0}\r\n效果：{1}", entry, AllDictionary.Dic_EntryInfo[entry].description), delegate
            {
                confirm = true;
                var count = chara.entrys.Count;
                __instance.OnAdd(entry, __args[1] as string);
                instance.Logger.LogInfo("EntryManager_OnAdd  " + chara.name + " chara.entrys.Count " + count + "|" + chara.entrys.Count);

                if (chara.entrys.Count > count)
                {
                    chara.maxAddEntryNum = chara.maxAddEntryNum - 1;
                }
                else if (chara.entrys.Count < count)
                {
                    chara.maxAddEntryNum = chara.maxAddEntryNum + 1;
                }
                instance.Logger.LogInfo("EntryManager_OnAdd " + chara.name + "maxAddEntryNum " + chara.maxAddEntryNum);
            }, delegate
            {
                WholeObjects.Instance.GetPlayerSect().gongDe = GongDe;
                confirm = false;
                var holdCoinNumField = sacrificePanel.GetType().GetField("holdCoinNum", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.IgnoreCase);
                if (holdCoinNumField != null)
                {
                    var holdCoinNum = holdCoinNumField.GetValue(sacrificePanel) as Text;
                    if (holdCoinNum != null)
                    {
                        holdCoinNum.text = GongDe.ToString();
                    }
                }
            }, "确认", "取消");

            return confirm;

        }

        [HarmonyPostfix]
        [HarmonyPatch(typeof(Chara))]
        [HarmonyPatch("Chara", MethodType.Constructor)]
        public static void Chara_Chara_Postfix(Chara __instance)
        {
            //__instance.maxAddEntryNum = __instance.maxAddEntryNum + instance.maxAddEntryNum.Value;
            //instance.Logger.LogInfo("初始化一个角色" + __instance.maxAddEntryNum);

        }

    }
}
